﻿using System;
using System.Globalization;

namespace Common.Extention
{
    public static class DateTimeExtention
    {
        /// <summary>
        /// 转换年月日格式，如1900-07-01
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ConvertDateToString(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd");
        }
        /// <summary>
        /// 转换年月日小时分秒格式，如19000701120030
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ConvertDateWithSecondToString(this DateTime dateTime)
        {
            return dateTime.ToString("yyyyMMddHHmmss");
        }
        /// <summary>
        /// 获取当前日期是一年当中的第几周
        /// </summary>
        /// <param name="dtime"></param>
        /// <returns></returns>
        public static int GetWeekofYear(this DateTime dtime)
        {
            GregorianCalendar gc = new GregorianCalendar();
            return gc.GetWeekOfYear(dtime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        }
        /// <summary>
        /// 转换时间戳
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static long UnixTimestamp(this DateTime source)
        {
            return (source.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
        }
    }
}
